<?php

  function db_connect($db_name='p301',$host='127.0.0.1',$db_username='root',$db_password='root'){     //默认值

  $link = mysqli_connect($host, $db_username, $db_password, $db_name);

  //检测数据库是否链接成功
  if (!$link) {
      echo '<script> alert("您的数据库链接失败。请检查")</script>';
      die;
  }
 

  //$link数据库对象 mysqli_query运行数据命令
  mysqli_query($link, 'set names utf8');
     return $link;
}

function del($table,$where=array()){
  if (is_array($where)) {    //是否是数组
      $str =implode(',',$where);// implode(1,2)  1可选。规定数组元素之间放置的内容。默认是 ""还可以是 ,. 由引号包裹（空字符串）
    # code...
      $where="in ($str)";     
  }else{
    $where='='.$where;
  }
 $sql = "delete from `$table` where id $where";

  mysqli_query($GLOBALS['link'],$sql);

  return mysqli_affected_rows($GLOBALS['link']);
}
function selectone($table,$where,$file='*'){
  if(is_array($where)){
    $str=''; 
    $i=0;
    foreach($where as $k=>$v) {

        if($i==count($where)-1){
         $str .="`$k`='$v'";
        }else{
         $str .="`$k`='$v' and" ;   
        }
          $i++;
    }         
    $where=$str;
  }
  $sql="select $file from `$table` where  $where limit 1";
    $result=mysqli_query($GLOBALS['link'],$sql);
    return mysqli_fetch_assoc($result);  
  # code...
}

function add($table,$where=array()){

  $v="'".implode("','",array_values($where))."'";

  $k="`".implode("`,`",array_keys($where))."`";  //array_values取数组值（非键名）array_keys取下标建名

 $sql="insert into `$table` ($k)values($v)";
  // $sql="insert into `$table`(`username`,`sex`,`age`)values('admin','')";
  
    mysqli_query($GLOBALS['link'],$sql);
//判断mysqli_query语句是否执行成功
    return mysqli_affected_rows($GLOBALS['link']);
}     
          
function selectmore($table,$where=1,$file='*',$order='',$limit=''){//$where=1 值为真（>0）表示查询全部
  $where1='';
  if (!empty($limit)) {
    $where1.='limit 8';
    # code...
  }
   if (!empty($order)) {
    $where1.='order by'.$order;
    # code...
  }
   $sql="select $file from `$table` where $where $where1";
  $result=mysqli_query($GLOBALS['link'],$sql);
while ($arr=mysqli_fetch_assoc($result)) {
  $data[]=$arr;
  # code...
}
  return $data;  
  # code...
}


function getSelect($tab,$file='*',$where='1=1',$order='',$limit=''){
        if(!empty($order)){
            $order='order by '.$order;
        }
        if(!empty($limit)){
            $limit='limit '.$limit;
        }
        if(is_array($where)){
            $str='';
            foreach ($where as $k => $v) {
                $str.=" $k = '$v' and";
            }
            $str.=' 1=1';
            $where=$str;
        }
         $sql="select $file from $tab where $where $order $limit  ";

                //取出数据
        $result=mysqli_query($GLOBALS['link'],$sql);
                //用while循环把数据放入到数组中去
        if(!empty($result)){
            while($arr = mysqli_fetch_assoc($result)){
                $data[]= $arr;
            }
        }
        return $data;
    }



function edxit($table,$where=array(),$data=array()){

  
  if (is_array($where)) {    //是否是数组
      $str =implode(',',$where);// implode(1,2)  1可选。规定数组元素之间放置的内容。默认是 ""还可以是 ,. 由引号包裹（空字符串）
    # code...
      $where="in ($str)";     
  }else{
    $where='='.$where;
  }

  $filed = '';
  foreach ($data as $key => $v) {
    $filed.="`$key`='$v',";
  }
 
  $filed = substr($filed, 0,-1);

$sql="update `$table` set  $filed where id $where ";
 
  // $sql="insert into `$table`(`username`,`sex`,`age`)values('admin','')";
  
    mysqli_query($GLOBALS['link'],$sql);
//判断mysqli_query语句是否执行成功
    return mysqli_affected_rows($GLOBALS['link']);}